# -*- coding: utf_8 -*-
# @Time : 2021/8/9 0009 15:47
# @Author : lyw
# @Remark : 门禁公共接口
import time

from mxsoftpy import Model
from pydantic import validator
import re


class RecDoorModel(Model):
    """门禁"""
    auth_method: str = ""
    # origin_id: str
    device_id: str
    host: str
    is_del: int = 0
    label: str
    model_id: str

    @validator('label', allow_reuse=True)
    def check_label(cls, value):
        assert value != "", "label参数不存在..."
        return value

    @validator('host', allow_reuse=True)
    def check_host(cls, value):
        pattern = re.compile(r'^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$')
        re_str = pattern.match(value)
        if re_str:
            return re_str.group()
        else:
            raise AssertionError("IP地址格式不正取...")

    # @validator('origin_id')
    # def check_origin_id(cls, value):
    #     assert value.isdigit(), "origin_id参数不正确..."
    #     return value

    @validator('device_id', allow_reuse=True)
    def check_device_id(cls, value):
        assert value != "", "device_id参数不正确..."
        return value


class RecordModel(Model):
    """出入记录"""
    create_time: int
    door_id: str
    motion: str
    pers_id: str
    name: str

    # model_id: str

    @validator('motion', allow_reuse=True)
    def check_motion(cls, value):
        assert value != "", "motion参数不存在..."
        return value

    @validator('create_time', allow_reuse=True)
    def check_create_time(cls, value):
        assert value != 0, "create_time参数不存在..."
        return value


class PersonnelModel(Model):
    """人员"""
    create_time: int = int(time.time())
    door_id: str
    is_del: int = 0
    name: str
    serial_num: str
    state: str = 0
    fingerprint: str = ""
    update_time = int(time.time())

    @validator('serial_num', allow_reuse=True)
    def check_serial_num(cls, value):
        assert value.isdigit(), "serial_num参数不存在..."
        return value
